package com.lkh.real;

import com.lkh.common.Config;
import com.lkh.map.GenderJoinUserMapper;
import org.apache.flink.api.common.functions.FilterFunction;
import org.apache.flink.api.common.serialization.SimpleStringSchema;
import org.apache.flink.api.java.tuple.Tuple3;
import org.apache.flink.streaming.api.datastream.DataStreamSource;
import org.apache.flink.streaming.api.datastream.SingleOutputStreamOperator;
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;
import org.apache.flink.streaming.connectors.kafka.FlinkKafkaConsumer;

import java.util.Properties;

public class ComputeGenderIndexApp {
    public static void main(String[] args) throws Exception {

        /*
         * 计算性别占比
         *
         */

        StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment();

        Properties kafkaProps = new Properties();
        kafkaProps.setProperty("zookeeper.connect", Config.getString("kafka.zookeeper.connect"));
        kafkaProps.setProperty("bootstrap.servers", Config.getString("kafka.bootstrap.servers"));
        kafkaProps.setProperty("group.id", "asdasdsasdssad");

        //构建kafkaSource
        FlinkKafkaConsumer<String> kafkaConsumer = new FlinkKafkaConsumer<>(
                "com",
                new SimpleStringSchema(),
                kafkaProps);

        kafkaConsumer.setStartFromEarliest();

        DataStreamSource<String> dataStreamSource = env.addSource(kafkaConsumer);

        SingleOutputStreamOperator<String> filter = dataStreamSource.filter(new FilterFunction<String>() {
            @Override
            public boolean filter(String value) throws Exception {

                return value.split("\\|").length == 6;
            }
        });

        SingleOutputStreamOperator<Tuple3<String, String, Integer>> joinDS = filter.map(new GenderJoinUserMapper());

        joinDS.keyBy(0, 1)
                .sum(2)
                .addSink(new GenderRedisSink());


        env.execute();
    }
}
